<?php
    /*

     Version: MPL 1.1

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is KASSIOP Framework.

     The Initial Developer of the Original Code is SURIAN Nicolas (aka NairuS) <me@nairus.fr>.
     Portions created by the Initial Developer are Copyright (C) 2010
     The Initial Developer. All Rights Reserved.

     Contributor(s):

     Alternatively, the contents of this file may be used under the terms
     of the MPL license Version 1.1 (the  "MPL" License"), in which case the
     provisions of Version: MPL 1.1 License are applicable instead of those
     above.  If you wish to allow use of your version of this file only
     under the terms of the MPL License and not to allow others to use
     your version of this file under the MPL, indicate your decision by
     deleting  the provisions above and replace  them with the notice and
     other provisions required by the MPL License.  If you do not delete
     the provisions above, a recipient may use your version of this file
     under either the MPL License.

     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

    */


    require_once 'PHPUnit/Framework.php';
    require_once dirname( __FILE__ ) . '/../../../kassiop/core/Autoload.php' ;
    Autoload::register() ;

    /**
     * The unit test class of the CredentialManager class.
     *
     * @package    unittests.kassiop
     * @subpackage managers
     *
     * @author NairuS
     * @version 1.2 $Rev: 90 $ $Date: 2011-01-16 09:23:26 +0000 (Sun, 16 Jan 2011) $ $Author: nicolas.surian $
     *
     */
    class CredentialsManagerTest extends PHPUnit_Framework_TestCase
    {
        /**
         * Defines the CredentialsManagerClass to test.
         *
         * @var CredentialsManagerClass
         */
        protected $manager ;

        /**
         * Defines the ConfigManager instance.
         *
         * @var ConfigManager
         */
        protected $configManager;

        protected function setUp()
        {
            $this->configManager = new ConfigManager();
            $this->manager       = new CredentialsManagerClass( $this->configManager->getFrontConfigPath() . 'credentials.properties' ) ;
        }

        protected function changeManager()
        {
            Configuration::includeBootstrap() ;
            $this->manager = new CredentialsManagerClass( $this->configManager->getBackConfigPath() . 'credentials.properties' ) ;
        }

        protected function tearDown()
        {
            unset( $this->manager );
        }

        public function testGetCredentials()
        {
            $credentials = $this->manager->getCredentials() ;

            $this->assertTrue( !empty( $credentials ), 'The credentials must be setted.' ) ;
            $this->assertEquals( count( $credentials ), 4, 'The credentials must be 3.' ) ;
        }

        public function testGetCredential()
        {
            // FRONT CREDENTIALS

            $this->assertEquals(    $this->manager->getCredential( 'MyController' ), 'contributor', '1.1 The credential must be declared' );
            $this->assertEquals(    $this->manager->getCredential( 'MyController.notDeclared' ), 'contributor', '1.2 The credential must take the parent one' );
            $this->assertEquals(    $this->manager->getCredential( 'MyController.myAction' ), 'admin', '1.3 The credential must be declared' );
            $this->assertEquals(    $this->manager->getCredential( 'MyService.myMethod' ), 'user,admin', '1.4 The credential must be declared' );
            $this->assertNotEquals( $this->manager->getCredential( 'OverrideCredential' ), 'admin', '1.5 The credential must be overrided' );
            $this->assertEquals(    $this->manager->getCredential( 'OverrideCredential' ), 'user', '1.6 The credential must be declared' );

            // CHANGE MANAGER

            $this->tearDown();
            $this->changeManager();

            // BACK CREDENTIALS

            $this->assertEquals( $this->manager->getCredential( 'MyControllerAdmin' ), 'contributor', '2.1 The credential must be declared' );
            $this->assertEquals( $this->manager->getCredential( 'MyControllerAdmin.notDeclared' ), 'contributor', '2.2 The credential must take the parent one' );
            $this->assertEquals( $this->manager->getCredential( 'MyControllerAdmin.myAction' ), 'admin', '2.3 The credential must be declared' );
            $this->assertEquals( $this->manager->getCredential( 'MyServiceAdmin.myMethod' ), 'user,admin', '2.4 The credential must be declared' );

            // RESET MANAGER

            $this->tearDown();
            $this->setUp();
        }

        public function testGetRole()
        {
            $this->assertEquals( $this->manager->getRole( 'admin' )      , new RoleVO( array( 'slug' => 'admin'      , 'rank' => 9 ) ), 'The "admin" role slug must return his RoleVO.' ) ;
            $this->assertEquals( $this->manager->getRole( 'contributor' ), new RoleVO( array( 'slug' => 'contributor', 'rank' => 2 ) ), 'The "contributor" role slug must return his RoleVO.' ) ;
            $this->assertEquals( $this->manager->getRole( 'user' )       , new RoleVO( array( 'slug' => 'user'       , 'rank' => 1 ) ), 'The "user" role slug must return his RoleVO.' ) ;
            $this->assertEquals( $this->manager->getRole( 'users' )      , null, 'The "users" role slug must return null.' ) ;
        }

        public function testGetRoles()
        {
            $roles = $this->manager->getRoles() ;

            $this->assertTrue( !empty( $roles ), 'The roles must be setted.' ) ;
            $this->assertEquals( count( $roles ), 3, 'The roles must be 3.' ) ;
        }

        public function testHasCredential()
        {
            $userRole = new RoleVO( array( 'slug' => 'admin', 'rank' => 9 ) ) ;
            $this->assertTrue( $this->manager->hasCredential( '', $userRole ), '1.1- No credential defined, must return true.' ) ;
            $this->assertTrue( $this->manager->hasCredential( 'MyController', $userRole ), '1.2- Credential defined, must return true.' ) ;
            $this->assertTrue( $this->manager->hasCredential( 'MyController.myAction', $userRole ), '1.3- Credential defined, must return true.' ) ;
            $this->assertTrue( $this->manager->hasCredential( 'MyService.myMethod', $userRole ), '1.4- Credential defined, must return true.' ) ;
            $this->assertTrue( $this->manager->hasCredential( 'OverrideCredential', $userRole ), '1.5- Credential defined, must return true.' ) ;

            $userRole = new RoleVO( array( 'slug' => 'contributor', 'rank' => 2 ) ) ;
            $this->assertTrue(  $this->manager->hasCredential( '', $userRole ), '2.1- No credential defined, must return true.' ) ;
            $this->assertTrue(  $this->manager->hasCredential( 'MyController', $userRole ), '2.2- Credential defined, must return true.' ) ;
            $this->assertFalse( $this->manager->hasCredential( 'MyController.myAction', $userRole ), '2.3- Credential defined, must return false.' ) ;
            $this->assertFalse( $this->manager->hasCredential( 'MyService.myMethod', $userRole ), '2.4- Credential defined, must return false.' ) ;
            $this->assertTrue(  $this->manager->hasCredential( 'OverrideCredential', $userRole ), '2.5- Credential defined, must return true.' ) ;

            $userRole = new RoleVO( array( 'slug' => 'user', 'rank' => 1 ) ) ;
            $this->assertTrue(  $this->manager->hasCredential( '', $userRole ), '3.1- No credential defined, must return true.' ) ;
            $this->assertFalse( $this->manager->hasCredential( 'MyController', $userRole ), '3.2- Credential defined, must return false.' ) ;
            $this->assertFalse( $this->manager->hasCredential( 'MyController.myAction', $userRole ), '3.3- Credential defined, must return false.' ) ;
            $this->assertTrue(  $this->manager->hasCredential( 'MyService.myMethod', $userRole ), '3.4- Credential defined, must return true.' ) ;
            $this->assertTrue(  $this->manager->hasCredential( 'OverrideCredential', $userRole ), '3.5- Credential defined, must return true.' ) ;

            $this->assertFalse( $this->manager->hasCredential( 'MyController.noCredentials' ), '4.1- Credentials needed for this action because of his parent.' ) ;
            $this->assertFalse( $this->manager->hasCredential( 'MyController' ), '4.2- Credentials needed for this action and RoleVO must be setted.' ) ;
            $this->assertFalse( $this->manager->hasCredential( 'MyController.myAction' ), '4.3- Credentials needed for this action and RoleVO must be setted.' ) ;
            $this->assertFalse( $this->manager->hasCredential( 'MyService.myMethod' ), '4.4- Credentials needed for this action and RoleVO must be setted.' ) ;
            $this->assertFalse( $this->manager->hasCredential( 'OverrideCredential' ), '4.5- Credentials needed for this action and RoleVO must be setted.' ) ;
            $this->assertTrue(  $this->manager->hasCredential( 'NoCredential.test' ), '4.6- No credential need for this action not declared.' ) ;
        }
    }
?>